/**
 * Created with JetBrains WebStorm.
 * User: thaidoanhong
 * Date: 5/8/13
 * Time: 1:21 PM
 * To change this template use File | Settings | File Templates.
 */
/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 4/22/13
 * Time: 2:11 PM
 * To change this template use File | Settings | File Templates.
 */
var MyVideo = Class.create(MyElement, {
    initialize: function($super, o){
        $super(o);
        this.className = "MyVideo";
    },
    initAttributeList: function($super, o){
        $super(o);
        var _this = this;

        $j.each(o.element, function(){
           if(this.type == "video" && this.key == "src"){
               _this.attributeList.add(new MyAttribute(this));
               if(_this.videoTag == undefined){
                   _this.videoTag = new Element("video", {preload: "none"});
                   $j(_this.videoTag).prop("controls", true);
                   _this.videoTag.setStyle({width: "100%", height: "100%"});
                   $(_this.divTag).insert(_this.videoTag);
               }

                var sourceVideo = $(_this.videoTag).firstDescendant();
                if(sourceVideo == null){
                    sourceVideo = $j("<source type='video/mp4'>");
                    $j(_this.videoTag).append(sourceVideo);

                }
               $j(sourceVideo).attr(this.key, this.value);
            }
            else if(this.type == "video" && this.key == "poster"){
               _this.attributeList.add(new MyAttribute(this));

               $j(_this.videoTag).attr("poster", this.value)
           }
        });
    },
    reloadAttributeList: function($super, o){
        $super(o);
        var _this = this;

        $j.each(o.element, function(){
            if(this.key == "src"){
                _this.attributeList.add(new MyAttribute(this));

                var sourceVideo = $(_this.videoTag).firstDescendant();
                sourceVideo.setAttribute(this.key, this.value);
            }
            if(this.key == "poster"){
                _this.attributeList.add(new MyAttribute(this));

                $j(_this.videoTag).attr("poster", this.value);

            }
        });
    },
    setValueByKey: function($super, key, value){
        if($super(key, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromKey(key);
            if(att !== null){
                var matched = false;
                if(att.type == "video" && att.key == "src"){

                    var sourceVideo = $(this.videoTag).firstDescendant();
                    if(sourceVideo !== null){
                        $(sourceVideo).setAttribute(att.key, value);
                    }
                    // load new source video
                    this.videoTag.load();
                    matched = true;
                }
                else if(att.type == "video" && att.key == "poster"){
                    $j(this.videoTag).attr("poster", value);
                    matched = true;
                }
                if(matched){
                    this.attributeList.setValueByKey(key, value);
                    return true;
                }
            }
        }
        return false;
    },
    setValueByName: function($super, name, value){
        if($super(name, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromName(name);
            if(att !== null){
                var matched = false;
                if(att.type == "video" && att.key == "src"){

                    var sourceVideo = $(this.videoTag).firstDescendant();
                    if(sourceVideo !== null){
                        $(sourceVideo).setAttribute(att.key, value);
                    }
                    // load new source video
                    this.videoTag.load();
                    matched = true;
                }
                else if(att.type == "video" && att.key == "poster"){
                    $j(this.videoTag).attr("poster", value);
                    matched = true;
                }
                if(matched){
                    this.attributeList.setValueByName(name, value);
                    return true;
                }
            }
        }
        return false;
    }
});